package cn.kduck.code.entity.service;

import cn.kduck.core.service.ValueMap;
import cn.kduck.core.utils.StringUtils;

import java.util.Map;

import static cn.kduck.code.entity.service.Entity.EntityFieldEnum.*;

public class Entity extends ValueMap {

    public Entity() {
    }

    public Entity(Map map) {
        if (map != null) {
            super.putAll(map);
        }
    }

    public void setEntityId(String entityId) {
        super.put(ENTITY_ID.getAttrName(), entityId);
    }

    public String getEntityId() {
        return super.getValueAsString(ENTITY_ID.getAttrName());
    }

    public void setEntityName(String entityName) {
        super.put(ENTITY_NAME.getAttrName(), entityName);
    }

    public String getEntityName() {
        return super.getValueAsString(ENTITY_NAME.getAttrName());
    }

    public void setFieldCount(Integer fieldCount) {
        super.put(FIELD_COUNT.getAttrName(), fieldCount);
    }

    public Integer getFieldCount() {
        return super.getValueAsInteger(FIELD_COUNT.getAttrName());
    }

    public void setEntityCode(String entityCode) {
        super.put(ENTITY_CODE.getAttrName(), entityCode);
    }

    public String getEntityCode() {
        return super.getValueAsString(ENTITY_CODE.getAttrName());
    }

    public enum EntityFieldEnum {
        ENTITY_ID("ENTITY_ID", "entityId"),
        ENTITY_CODE("ENTITY_CODE", "entityCode"),
        ENTITY_NAME("ENTITY_NAME", "entityName"),
        FIELD_COUNT("FIELD_COUNT", "fieldCount");

        private final String fieldName;
        private final String attrName;

        EntityFieldEnum(String fieldName, String attrName) {
            this.fieldName = fieldName;
            this.attrName = attrName;
        }

        public String getFieldName() {
            return fieldName;
        }

        public String getAttrName() {
            return attrName;
        }
    }

    @Override
    public String toString() {
        return getEntityName() + "实体，表名为" + getEntityCode() + "，对应实体类名为：" + StringUtils.upperCaseWithSeparator(getEntityCode(), '_');
    }
}
